/**
 * SMS 공지에서 쓰는 JS
 */

var env = {
	contextPath : ""
};

function setContextPath(paramContextPath){
	env.contextPath = paramContextPath;
}

/**
 * 자주 쓰는 페이지 이동.
 * @param page
 * @return
 */
function gotoPage(page) {
	var url = "";
	/*
	 * SMS 보내기
	 */
	if (page == 'SMS_SEND') url = env.contextPath + "/notice/smsNotice.omp?topCode=M003&leftCode=M003006001";
	/*
	 * SMS 전송이력
	 */
	else if (page == "SMS_LIST") url = env.contextPath + "/notice/smsList.omp?topCode=M003&leftCode=M003006001";
	
	location.href= url;
}

//callback SMS 선택 이벤트
var toggleCallbackUrl = function() {
	if ($(":input:radio[name=smsType]:checked").val() == 'callback') {
		document.getElementById("body").value = "";
		document.getElementById("callbackUrl").value = "";
		document.getElementById("bodyLen").innerHTML = "";
		$("#callbackUrlArea").show();
	} else {
		document.getElementById("body").value = "";
		document.getElementById("callbackUrl").value = "";
		document.getElementById("bodyLen").innerHTML = "";
		$("#callbackUrlArea").hide();
	}
};

//예약발송타입 선택 이벤트
var toggleReserveType = function() {
	if ($(":input:radio[name=reserveType]:checked").val() == 'reserved') {
		$("#reservedArea").show();
		$("#reservedArea2").show();
	} else {
		$("#reservedArea").hide();
		$("#reservedArea2").hide();
		// 예약일 초기화
		$("#sendDate").val("");
	}
};

// 수신타입 선택 이벤트
var toggleSmsType = function() {
	if ($(":input:radio[name=receiverType]:checked").val() == 'appoint') {
		//$("#appointArea1").show();
		$("#appointArea_group").show();
		//$("#appointArea_file").show();
		$("#basicArea1").hide();
		$("#basicArea2").hide();
	} else {
		//$("#appointArea1").hide();
		$("#appointArea_group").hide();
		//$("#appointArea_file").hide();
		$("#basicArea1").show();
		$("#basicArea2").show();
		$("input[name=receiverClass]").filter('input[value=group]').attr("checked", "checked");
	}
};
// 대상조건 지정 선택 이벤트
var toggleReceiverClass = function() {
	if ($(":input:radio[name=receiverClass]:checked").val() == 'group') {
		$("#appointArea_group").show();
		$("#appointArea_file").hide();
	} else {
		$("#appointArea_file").show();
		$("#appointArea_group").hide();
	}
};
// 지우기
var clearText = function(loc) {
	$("#"+loc).val("");
};

// 검색한 회원 정보 세팅
var setReceiver = function(mobileNo, name, mbrNo) {
	//alert("mobileNo:"+mobileNo+", name:"+name + ", mbrNo:" + mbrNo);
	var mdn = mobileNo.split(",");
	var nm = name.split(",");
	var id = mbrNo.split(",");
	var i = 0;

	$("input[name=receiver]").each(function(){
		if ($(this).val() == "") {
			$(this).val(mdn[i]);
			$("#n"+$(this).attr("id").substring(1)).val(nm[i]);
			$("#id"+$(this).attr("value").substring(1)).val(id[i]);
			
			i++;
		}
	});
};

//검색한 회원 정보 세팅
var setReceiver = function(mobileNo, name, mbrNo,mnoCd) {
	if(mnoCd == null){
		setReceiver(mobileNo, name, mbrNo);
	}else{
//		alert("mobileNo:"+mobileNo+", name:"+name + ", mbrNo:" + mbrNo + ", mnoCd:" + mnoCd);
		var mdn = mobileNo.split(",");
		var nm = name.split(",");
		var id = mbrNo.split(",");
		var mno = mnoCd.split(",");
		var i = 0;
		$("input[name=receiver]").each(function(){
			if ($(this).val() == "") {
				$(this).val(mdn[i]);
				$("#n"+$(this).attr("id").substring(1)).val(nm[i]);
				$("#id"+$(this).attr("value").substring(1)).val(id[i]);
				$("#m"+$(this).attr("id").substring(1)).val(mno[i]);
				i++;
			}
		});
	}
};
var popMemberSearchStart = function() {
	//popMemberSearch($("#keyword").val());
	
	var params = {
			searchValue : isNull($("#keyword").val()) ? '' : $("#keyword").val(),
			searchType : 'member.nameId'
		};
		$.ajaxSetup({
			global: true,
		    beforeSend: function(xhr) {} ,
			success: function(data) {}
		});
		$.ajax({
			type: "POST",
			url: env.contextPath + "/memberDev/popMemberSearch.omp",
			data: params,
			beforeSend: function(xhr) {},
			success: function(data) {
				createPopupLayer("popMemberSearch");
				$("#popMemberSearch_body").html(data);
				showPopupLayer("popMemberSearch", "container");
			}
		});
		$.ajaxSetup({
			global: true,
		    beforeSend: function(xhr) {} ,
			success: function(data) {}
		});
};

function getByteLength(str){
	 var resultSize = 0;
	 if (str == null) {
		return 0;
	}

	for ( var i = 0; i < str.length; i++) {
		var c = escape(str.charAt(i));
		if (c.length == 1) {
			resultSize++;
		} else if (c.indexOf("%u") != -1) {
			resultSize += 2;
		} else if (c.indexOf("%") != -1) {
			resultSize += c.length / 3;
		}
	}
	return resultSize;
} 

// 문자 내용 최대 80 바이트. 콜백인 경우  text 30 / url 50 바이트.

var checkLength = function() {
	var body = $("#body").val();
	var url = $("#callbackUrl").val();
	var smsType = $(":input:radio[name=smsType]:checked").val();
	
	var bodyLength = getByteLength(body);
    var urlLength = getByteLength(url);	
	if (smsType == 'normal') {
		if(bodyLength > 80){
			isLimitByteMessage(document.getElementById("body"),80,'80byte 까지 입력가능합니다.');
			document.getElementById("bodyLen").innerHTML = "80/80";	
		}
		else if(bodyLength <= 80){
			document.getElementById("bodyLen").innerHTML = getByteLength(body) + "/80";
		}
	} else if (smsType == 'callback') {
		isLimitByteMessage(document.getElementById("body"),30,'30byte 까지 입력가능합니다.');
		isLimitByteMessage(document.getElementById("callbackUrl"),50,'50byte 까지 입력가능합니다.');
		if(bodyLength > 30){
			var tamp = 30+getByteLength($("#callbackUrl").val());
			document.getElementById("bodyLen").innerHTML = tamp + "/80";
		}
		else if(urlLength > 50){
			var temp = 50+getByteLength($("#body").val());
			document.getElementById("bodyLen").innerHTML = temp + "/80";
		}
		else{
			document.getElementById("bodyLen").innerHTML = (bodyLength+urlLength) + "/80";
		}
	}
	//document.getElementById("bodyLen").innerHTML = (getByteLength(body) + getByteLength(url)) + "/80";
	//document.getElementById("urlLen").innerHTML = getByteLength(url);
};

function isLimitByteMessage(obj,limitMax,msg) {
	
	var temp = 0;
	var onechar;
	var tcount = 0;
	var aquery = obj.value; 
	var tmpStr2 = "";

	tmpStr = new String(aquery);
	temp = tmpStr.length;
	
	for (var k=0;k<temp;k++){
		onechar = tmpStr.charAt(k);
		if (escape(onechar).length > 4) {
			tcount += 2;
		}else{
			tcount++;
		}
		if(tcount>limitMax) {
			if(escape(tmpStr.charAt(tcount-2)).length == 3){
				k=k-1;
			}
			tmpStr2 = tmpStr.substring(0,k);
			break;
		}else{
			tmpStr2 += onechar; 
		}
	}		

	if(tcount > Number(limitMax)) {
		reserve = tcount-Number(limitMax);
		alert(msg);
		obj.value = tmpStr2;
	return;
	}
}
